<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Exception correctness</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Boost.Test">
<link rel="up" href="../testing_tools.html" title="Writing unit tests">
<link rel="prev" href="extended_comparison/bitwise.html" title="Bitwise comparison">
<link rel="next" href="timeout.html" title="Time-out for test cases">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="extended_comparison/bitwise.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../testing_tools.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="timeout.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_test.testing_tools.exception_correctness"></a><a class="link" href="exception_correctness.html" title="Exception correctness">Exception
      correctness</a>
</h3></div></div></div>
<p>
        Any unexpected/uncaught exception raised in the test case body will be intercepted
        by the Boost.test framework and will result in the termination of the test-case
        with the status <code class="computeroutput"><span class="identifier">failed</span></code>.
      </p>
<h6>
<a name="boost_test.testing_tools.exception_correctness.h0"></a>
        <span class="phrase"><a name="boost_test.testing_tools.exception_correctness.example_descr"></a></span><a class="link" href="exception_correctness.html#boost_test.testing_tools.exception_correctness.example_descr">Example:
        Uncaught exception</a>
      </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                <p>
                  Code
                </p>
              </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">struct</span> <span class="identifier">my_struct</span> <span class="special">{</span>
  <span class="identifier">my_struct</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">var_</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">var</span><span class="special">(</span><span class="identifier">var_</span><span class="special">)</span>
  <span class="special">{</span>
    <span class="keyword">if</span><span class="special">(</span><span class="identifier">var_</span> <span class="special">&lt;</span> <span class="number">0</span><span class="special">)</span> <span class="keyword">throw</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span><span class="special">(</span><span class="string">"negative value not allowed"</span><span class="special">);</span>
  <span class="special">}</span>
  <span class="keyword">int</span> <span class="identifier">var</span><span class="special">;</span>
<span class="special">};</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span> <span class="identifier">test</span> <span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">my_struct</span> <span class="identifier">instance</span><span class="special">(-</span><span class="number">2</span><span class="special">);</span>
  <span class="comment">// ...</span>
<span class="special">}</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span> <span class="identifier">test2</span> <span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="keyword">true</span><span class="special">);</span>
<span class="special">}</span>
</pre>
              </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                <p>
                  Output
                </p>
              </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span> <span class="special">--</span><span class="identifier">log_level</span><span class="special">=</span><span class="identifier">all</span>
<span class="identifier">Running</span> <span class="number">2</span> <span class="identifier">test</span> <span class="identifier">cases</span><span class="special">...</span>
<span class="identifier">Entering</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"example"</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">20</span><span class="special">:</span> <span class="identifier">Entering</span> <span class="identifier">test</span> <span class="keyword">case</span> <span class="string">"test"</span>
<span class="identifier">unknown</span> <span class="identifier">location</span><span class="special">:</span><span class="number">0</span><span class="special">:</span> <span class="identifier">fatal</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test"</span><span class="special">:</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span><span class="special">:</span> <span class="identifier">negative</span> <span class="identifier">value</span> <span class="keyword">not</span> <span class="identifier">allowed</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">20</span><span class="special">:</span> <span class="identifier">last</span> <span class="identifier">checkpoint</span><span class="special">:</span> <span class="string">"test"</span> <span class="identifier">entry</span><span class="special">.</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">20</span><span class="special">:</span> <span class="identifier">Leaving</span> <span class="identifier">test</span> <span class="keyword">case</span> <span class="string">"test"</span><span class="special">;</span> <span class="identifier">testing</span> <span class="identifier">time</span><span class="special">:</span> <span class="number">247u</span><span class="identifier">s</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">26</span><span class="special">:</span> <span class="identifier">Entering</span> <span class="identifier">test</span> <span class="keyword">case</span> <span class="string">"test2"</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">28</span><span class="special">:</span> <span class="identifier">info</span><span class="special">:</span> <span class="identifier">check</span> <span class="keyword">true</span> <span class="identifier">has</span> <span class="identifier">passed</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">26</span><span class="special">:</span> <span class="identifier">Leaving</span> <span class="identifier">test</span> <span class="keyword">case</span> <span class="string">"test2"</span><span class="special">;</span> <span class="identifier">testing</span> <span class="identifier">time</span><span class="special">:</span> <span class="number">103u</span><span class="identifier">s</span>
<span class="identifier">Leaving</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"example"</span><span class="special">;</span> <span class="identifier">testing</span> <span class="identifier">time</span><span class="special">:</span> <span class="number">492u</span><span class="identifier">s</span>

<span class="special">***</span> <span class="number">1</span> <span class="identifier">failure</span> <span class="identifier">is</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">the</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"example"</span>
</pre>
              </td></tr></tbody>
</table></div>
<p>
        More control over the exception correctness is often required, for instance
        to test that an expression is raising a specific exception, intentionally.
      </p>
<p>
        The <span class="emphasis"><em>Unit Test Framework</em></span> provides several assertions
        for testing a code with respect to the exceptions correctness. The following
        assertions are available:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            <a class="link" href="../utf_reference/testing_tool_ref/assertion_boost_level_no_throw.html" title="BOOST_&lt;level&gt;_NO_THROW"><code class="computeroutput"><span class="identifier">BOOST_</span><span class="special">&lt;</span><span class="identifier">level</span><span class="special">&gt;</span><span class="identifier">_NO_THROW</span></code></a> checks that no exception
            is raised from an expression,
          </li>
<li class="listitem">
            <a class="link" href="../utf_reference/testing_tool_ref/assertion_boost_level_throw.html" title="BOOST_&lt;level&gt;_THROW"><code class="computeroutput"><span class="identifier">BOOST_</span><span class="special">&lt;</span><span class="identifier">level</span><span class="special">&gt;</span><span class="identifier">_THROW</span></code></a> checks that an expression
            raises an exception of a specific type
          </li>
<li class="listitem">
            <a class="link" href="../utf_reference/testing_tool_ref/assertion_boost_level_exception.html" title="BOOST_&lt;level&gt;_EXCEPTION"><code class="computeroutput"><span class="identifier">BOOST_</span><span class="special">&lt;</span><span class="identifier">level</span><span class="special">&gt;</span><span class="identifier">_EXCEPTION</span></code></a> checks that an expression
            raises an exception of a specific type, a passes the exception instance
            to a predicate function for further validation (introspection for instance)
          </li>
</ul></div>
<p>
        The following example demonstrate how to use these tools to test the correct
        behavior of the API of <code class="computeroutput"><span class="identifier">FileWordHistogram</span></code>,
        in the constructor and the preconditions of the member function.
      </p>
<h6>
<a name="boost_test.testing_tools.exception_correctness.h1"></a>
        <span class="phrase"><a name="boost_test.testing_tools.exception_correctness.example_descr0"></a></span><a class="link" href="exception_correctness.html#boost_test.testing_tools.exception_correctness.example_descr0">Example:
        Exception correctness of an API</a>
      </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                <p>
                  Code
                </p>
              </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">stdexcept</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">fstream</span><span class="special">&gt;</span>

<span class="comment">//! Computes the histogram of the words in a text file</span>
<span class="keyword">class</span> <span class="identifier">FileWordHistogram</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
  <span class="comment">//!@throw std::exception if the file does not exist</span>
  <span class="identifier">FileWordHistogram</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">filename</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">is_processed</span><span class="special">(</span><span class="keyword">false</span><span class="special">),</span> <span class="identifier">fileStream_</span><span class="special">(</span><span class="identifier">filename</span><span class="special">)</span> <span class="special">{</span>
    <span class="keyword">if</span><span class="special">(!</span><span class="identifier">fileStream_</span><span class="special">.</span><span class="identifier">is_open</span><span class="special">())</span> <span class="keyword">throw</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span><span class="special">(</span><span class="string">"Cannot open the file"</span><span class="special">);</span>
  <span class="special">}</span>

  <span class="comment">//! @returns true on success, false otherwise</span>
  <span class="keyword">bool</span> <span class="identifier">process</span><span class="special">()</span> <span class="special">{</span>
    <span class="keyword">if</span><span class="special">(</span><span class="identifier">is_processed</span><span class="special">)</span> <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>

    <span class="comment">// ...</span>
    <span class="identifier">is_processed</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">;</span>
    <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
  <span class="special">}</span>

  <span class="comment">//!@pre process has been called with status success</span>
  <span class="comment">//!@throw std::logic_error if preconditions not met</span>
  <span class="identifier">std</span><span class="special">::</span><span class="identifier">map</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">&gt;</span>
  <span class="identifier">result</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
    <span class="keyword">if</span><span class="special">(!</span><span class="identifier">is_processed</span><span class="special">)</span>
      <span class="keyword">throw</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span><span class="special">(</span><span class="string">"\"process\" has not been called or was not successful"</span><span class="special">);</span>
    <span class="keyword">return</span> <span class="identifier">histogram</span><span class="special">;</span>
  <span class="special">}</span>

<span class="keyword">private</span><span class="special">:</span>
  <span class="keyword">bool</span> <span class="identifier">is_processed</span><span class="special">;</span>
  <span class="identifier">std</span><span class="special">::</span><span class="identifier">ifstream</span> <span class="identifier">fileStream_</span><span class="special">;</span>
  <span class="identifier">std</span><span class="special">::</span><span class="identifier">map</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">&gt;</span> <span class="identifier">histogram</span><span class="special">;</span>
<span class="special">};</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span> <span class="identifier">test_throw_behaviour</span> <span class="special">)</span>
<span class="special">{</span>
  <span class="comment">// __FILE__ is accessible, no exception expected</span>
  <span class="identifier">BOOST_REQUIRE_NO_THROW</span><span class="special">(</span> <span class="identifier">FileWordHistogram</span><span class="special">(</span><span class="identifier">__FILE__</span><span class="special">)</span> <span class="special">);</span>

  <span class="comment">// ".. __FILE__" does not exist, API says std::exception, and implementation</span>
  <span class="comment">// raises std::runtime_error child of std::exception</span>
  <span class="identifier">BOOST_CHECK_THROW</span><span class="special">(</span> <span class="identifier">FileWordHistogram</span><span class="special">(</span><span class="string">".."</span> <span class="identifier">__FILE__</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span> <span class="special">);</span>

  <span class="special">{</span>
    <span class="identifier">FileWordHistogram</span> <span class="identifier">instance</span><span class="special">(</span><span class="identifier">__FILE__</span><span class="special">);</span>

    <span class="comment">// api says "std::logic_error", implementation is wrong.</span>
    <span class="comment">// std::runtime_error not a child of std::logic_error, not intercepted</span>
    <span class="comment">// here.</span>
    <span class="identifier">BOOST_CHECK_THROW</span><span class="special">(</span><span class="identifier">instance</span><span class="special">.</span><span class="identifier">result</span><span class="special">(),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">logic_error</span><span class="special">);</span>
  <span class="special">}</span>
<span class="special">}</span>
</pre>
              </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                <p>
                  Output
                </p>
              </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span> <span class="special">--</span><span class="identifier">log_level</span><span class="special">=</span><span class="identifier">all</span>
<span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span>
<span class="identifier">Entering</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"example"</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">47</span><span class="special">:</span> <span class="identifier">Entering</span> <span class="identifier">test</span> <span class="keyword">case</span> <span class="string">"test_throw_behaviour"</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">50</span><span class="special">:</span> <span class="identifier">info</span><span class="special">:</span> <span class="identifier">check</span> <span class="char">'no exceptions thrown by FileWordHistogram("test")'</span> <span class="identifier">has</span> <span class="identifier">passed</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">54</span><span class="special">:</span> <span class="identifier">info</span><span class="special">:</span> <span class="identifier">check</span> <span class="char">'exception std::exception is caught'</span> <span class="identifier">has</span> <span class="identifier">passed</span>
<span class="identifier">unknown</span> <span class="identifier">location</span><span class="special">:</span><span class="number">0</span><span class="special">:</span> <span class="identifier">fatal</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_throw_behaviour"</span><span class="special">:</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span><span class="special">:</span> <span class="string">"process"</span> <span class="identifier">has</span> <span class="keyword">not</span> <span class="identifier">been</span> <span class="identifier">called</span> <span class="keyword">or</span> <span class="identifier">was</span> <span class="keyword">not</span> <span class="identifier">successful</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">62</span><span class="special">:</span> <span class="identifier">last</span> <span class="identifier">checkpoint</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">:</span><span class="number">47</span><span class="special">:</span> <span class="identifier">Leaving</span> <span class="identifier">test</span> <span class="keyword">case</span> <span class="string">"test_throw_behaviour"</span><span class="special">;</span> <span class="identifier">testing</span> <span class="identifier">time</span><span class="special">:</span> <span class="number">533u</span><span class="identifier">s</span>
<span class="identifier">Leaving</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"example"</span><span class="special">;</span> <span class="identifier">testing</span> <span class="identifier">time</span><span class="special">:</span> <span class="number">643u</span><span class="identifier">s</span>

<span class="special">***</span> <span class="number">1</span> <span class="identifier">failure</span> <span class="identifier">is</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">the</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"example"</span>
</pre>
              </td></tr></tbody>
</table></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
          An <code class="computeroutput"><span class="identifier">assert</span><span class="special">(...)</span></code>
          might call <code class="computeroutput"><span class="identifier">abort</span><span class="special">()</span></code>:
          the use of the macros above may not be suitable for testing for <code class="computeroutput"><span class="identifier">assert</span></code>'s in a portable manner.
        </p></td></tr>
</table></div>
<h5>
<a name="boost_test.testing_tools.exception_correctness.h2"></a>
        <span class="phrase"><a name="boost_test.testing_tools.exception_correctness.bibliographic_references"></a></span><a class="link" href="exception_correctness.html#boost_test.testing_tools.exception_correctness.bibliographic_references">Bibliographic
        references</a>
      </h5>
<div class="variablelist">
<p class="title"><b>Publications</b></p>
<dl class="variablelist">
<dt><span class="term"><a href="https://isocpp.org/wiki/faq/exceptions" target="_top">Exceptions and
          Error Handling</a></span></dt>
<dd><p>
              Standard C++ Foundation
            </p></dd>
</dl>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2022 Boost.Test contributors<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="extended_comparison/bitwise.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../testing_tools.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="timeout.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
